package thread;

import java.io.File;
import java.util.Scanner;

public class Demo54 {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("请输入要搜索的目录：");
        String file = scanner.next();
        File rootFile = new File(file);
        if (!rootFile.isDirectory()) {
            System.out.println("您输入的不是目录");
            return;
        }
        System.out.println("请输入关键字：");
        String keyWords = scanner.next();
        scanDir(rootFile, keyWords);
    }

    private static void scanDir(File rootFile, String keyWords) {
        //展开当前目录
        File[] files = rootFile.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            System.out.println("遍历目录&文件："+file.getAbsoluteFile());
            if (file.isFile()) {
                dealFile(file, keyWords);
            } else {
                scanDir(file, keyWords);
            }
        }
    }

    private static void dealFile(File file, String keyWords) {
        if (file.getName().contains(keyWords)) {
            System.out.println("找到了包含关键字的文件："+file.getAbsoluteFile()+"是否要删除：(y/n)");
            String input = scanner.next();
            if (input.equalsIgnoreCase("y")) {
                file.delete();
                System.out.println("删除成功");
            }
        }
    }
}
